<template>
  <!-- 导航栏 -->
  <van-nav-bar title="确认订单" left-arrow @click-left="back"> </van-nav-bar>
  <!-- 地址信息 -->
  <!-- 添加联系人 -->
  <van-contact-card
    type="add"
    @click="toAddressPage"
    v-if="JSON.stringify(address.value) == '{}'"
  />
  <!-- 编辑联系人 -->
  <van-contact-card
    type="edit"
    :tel="address.tel"
    :name="address.name"
    @click="toAddressPage"
    v-else
  />
  <!-- 商品信息 -->
  <van-card
    v-for="item in list"
    :key="item.id"
    :price="item.price"
    :desc="'库存:' + item.stock"
    :title="item.name"
    :thumb="item.thumb_text"
    @click-thumb="toProductInfo(item.proid)"
    :num="item.nums"
  >
    <template #tags>
      <div style="color: #969799">单价: {{ item.price }}</div>
    </template>
  </van-card>
  <!--订单备注 -->
  <van-field
    v-model="remark"
    rows="2"
    autosize
    label="订单备注"
    type="textarea"
    maxlength="120"
    placeholder="请输入订单备注"
    show-word-limit
  />
  <!--合计金额 -->
  <van-submit-bar
  v-for="item in list"
  :price="total"
  button-text="确认订单" @submit="onSubmit" />
</template>
<script setup>
import { computed, onBeforeMount } from "vue";

const { proxy } = getCurrentInstance();
const back = () => proxy.$router.go(-1);

let business = proxy.$cookies.get("business") ? proxy.$cookies.get("business") : {};
business = reactive(business);

let address = proxy.$cookies.get("address") ? proxy.$cookies.get("address") : {};
address = ref(address);

// console.log(address);
let proid = proxy.$route.query.hasOwnProperty("proid") ? proxy.$route.query.proid : 0;
proid = ref(proid);

const list = ref([]);
const remark = ref("");
const toAddressPage = () => {
  proxy.$router.push({
    path: "/address/index",
    query: { action: "order" },
  });
};

onBeforeMount(() => {
  if (JSON.stringify(address.value) == "{}") {
    getAddressData();
  }
  getCartData();
});

//获取商品数据
const getCartData = async () => {
  let result = await proxy.$POST({
    url: "/confirm/info",
    params: {
        busid: business.id,
        proid: proid.value,
    },
  });
  
  if (result.code == 1) {
    list.value = result.data;
  }
  // console.log(parseFloat(list.value.info.price))
};

//获取收货地址
const getAddressData = async () => {
  let result = await proxy.$POST({
    url: "/address/selected",
    params: {
      busid: business.id,
    },
  });
  if (result.code == 1) {
    address.value = result.data;
    proxy.$cookies.set("address", result.data);
  }
};

const total = computed(() => {
  let nums = list.value.info.price

  return nums*100;
})

//购买商品
const onSubmit = async () => {
  proxy.$confirm({
    message: "是否确认购买该商品？",
  })
  .then(async () => {
    let data = {
      busid: business.id,
      businessaddrid: address.value.id,
      amount: (total.value / 100).toFixed(2),
      remark: remark.value,
      proid: proid.value,
    };
    console.log(data)

    let result = await proxy.$POST({
      url: "/confirm/buy",
      params: data,
    });

    if(result && result.code == 0)
    {
      proxy.$fail(result.msg);
      return false;
    }else
    {
      proxy.$success({
        message: result.msg,
        duration: 1000,
        onClose: () => {
          proxy.$router.push('/order/index');
          return false;
        }
      })
    }
  })
  .catch(() => {});
  
};

//点击商品图片跳转详情页
const toProductInfo = (proid) => {
  proxy.$router.push({
    path: "/product/info",
    query: { proid: proid },
  });
};

</script>
